using System;
using System.Runtime.Serialization;

namespace KodeIT.Web
{
    /// <summary>
    /// This class represents the exception thrown when an http header name does not end with a colon (:).
    /// </summary>
    /// <remarks>
    /// Whenever dealing with http header names, the names must always end with a colon (:). Not doing so
    /// will cause this exception.
    /// </remarks>
    [Serializable]
    public class MalformedHeaderException : Exception
    {
        private string _headerName;

        /// <summary>
        /// Creates an instance of the <see cref="MalformedHeaderException"/> class.
        /// </summary>
        public MalformedHeaderException()
            : base()
        {
        }

        /// <summary>
        /// Creates an instance of the <see cref="MalformedHeaderException"/> class.
        /// </summary>
        /// <param name="headerName">The http header name that caused the exception.</param>
        public MalformedHeaderException(string headerName)
            : this(headerName, null)
        {
        }

        /// <summary>
        /// Creates an instance of the <see cref="MalformedHeaderException"/> class.
        /// </summary>
        /// <param name="headerName">The http header name that caused the exception.</param>
        /// <param name="nativeError">The native error associated with the exception.</param>
        public MalformedHeaderException(string headerName, int nativeError)
            : this(headerName, new NativeException(nativeError))
        {
        }

        /// <summary>
        /// Creates an instance of the <see cref="MalformedHeaderException"/> class.
        /// </summary>
        /// <param name="headerName">The http header name that caused the exception.</param>
        /// <param name="innerException">The inner exception to associate with the exception.</param>
        public MalformedHeaderException(string headerName, Exception innerException)
            : base(String.Format("The header name '{0}' does not end with colon (:)", headerName), innerException)
        {
            _headerName = headerName;
        }

        /// <summary>
        /// Creates an instance of the <see cref="MalformedHeaderException"/> class.
        /// </summary>
        /// <param name="info">The serialization information instance.</param>
        /// <param name="context">The streaming context instance.</param>
        protected MalformedHeaderException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Gets the http header name that caused the exception to be thrown.
        /// </summary>
        public string HeaderName
        {
            get { return _headerName; }
        }
    }
}
